package com.aibili.contoller.administrator;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.contoller.BaseController;
import com.aibili.pojo.EventInfo;
import com.aibili.pojo.HobbyInfo;
import com.aibili.pojo.LvInfo;
import com.aibili.pojo.MomentComment;
import com.aibili.pojo.MomentInfo;
import com.aibili.pojo.ReportInfo;
import com.aibili.pojo.UserAnonymous;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.aibili.service.EventService;
import com.aibili.service.MomentService;
import com.aibili.service.UserInfoService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Controller
@RequestMapping("/administrator")
public class UserController extends BaseController {

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private MomentService momentService;

	@Resource
	private EventService eventService;

	/**
	 * 
	 * @Description 条件查找全部的用户信息
	 * @param phone
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/userinfo")
	public ModelAndView userinfo(String mobile, String nick_name, String userid, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<UserInfo> results = userInfoService.getByParams(mobile, nick_name,
				CommonUtils.parseLong(userid, 0), pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<UserInfo> users = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", users);
			mav.addObject("page", page);
		}
		mav.addObject("mobile", mobile);
		mav.addObject("nick_name", nick_name);
		mav.addObject("userid", userid);
		return mav;
	}

	@RequestMapping("/userdetail")
	public ModelAndView userDetail(String userid) {
		ModelAndView mav = new ModelAndView();
		UserInfo user = userInfoService.getById(CommonUtils.parseLong(userid, 0));
		if (user != null) {
			user.setPortrait(user.getPortrait());
		}
		mav.addObject("obj", user);
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = "usercommentinfo", produces = "application/json; charset=utf-8")
	public ModelAndView userCommentInfo(String userid, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		PageBodyModel<MomentComment> results = momentService.getCommentByParams(CommonUtils.parseLong(userid, 0),
				pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<MomentComment> comments = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", comments);
			mav.addObject("page", page);
			
			if (comments != null && comments.size() > 0) {
				mav.addObject("user_name", comments.get(0).getUser().getUser_name());
			}
		}
		mav.addObject("userid", userid);
		return mav;
	}

	@RequestMapping("/usereventinfo")
	public ModelAndView userEventInfo(String userid, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<EventInfo> results = eventService.getByType(CommonUtils.parseLong(userid, 0), "", pageIndex,
				DsConstant.PAGESIZE);
		if (results != null) {
			List<EventInfo> events = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", events);
			mav.addObject("page", page);
			
			if (events != null && events.size() > 0) {
				mav.addObject("user_name", events.get(0).getUser().getUser_name());
			}
		}
		mav.addObject("userid", userid);
		return mav;
	}

	@ResponseBody
	@RequestMapping(value = "usermomentinfo", produces = "application/json; charset=utf-8")
	public ModelAndView userMomentInfo(String userid, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		PageBodyModel<MomentInfo> results = momentService.getMomentByParams(CommonUtils.parseLong(userid, 0),
				pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<MomentInfo> moments = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", moments);
			mav.addObject("page", page);
			
			if (moments != null && moments.size() > 0) {
				mav.addObject("user_name", moments.get(0).getUser().getUser_name());
			}
			
			
		}
		mav.addObject("userid", userid);
		return mav;
	}

	/**
	 * 
	 * @Description 兴趣爱好信息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/hobbyinfo")
	public ModelAndView hobbyInfo() {
		ModelAndView mav = new ModelAndView();

		List<HobbyInfo> infos = userInfoService.getHobbyList();
		mav.addObject("list", infos);
		return mav;
	}

	/**
	 * 
	 * @Description 兴趣爱好新增修改
	 * @param id
	 * @param hobby_name
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/hobbyedit", produces = "application/json; charset=UTF-8")
	public String hobbyedit(String id, String hobby_name, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(hobby_name)) {

			if (!CommonUtils.isEmptyString(id)) {
				HobbyInfo com = userInfoService.getHobbyByNameid(hobby_name, CommonUtils.parseLong(id, 0));
				if (com != null) {
					map.put("message", "兴趣名称已存在！");
					map.put("code", 1);
				} else {
					HobbyInfo info = userInfoService.getHobbyById(CommonUtils.parseLong(id, 0));
					if (info != null) {
						info.setHobby_name(hobby_name);
						userInfoService.updateHobby(info);
						map.put("code", 0);
					}
				}

			} else {
				HobbyInfo com = userInfoService.getHobbyByName(hobby_name);
				if (com != null) {
					map.put("message", "兴趣名称已存在！");
					map.put("code", 1);
				} else {
					HobbyInfo info = new HobbyInfo();
					info.setHobby_name(hobby_name);
					info.setCreate_time(new Date());
					userInfoService.addHobby(info);
					map.put("code", 0);
				}
			}
		} else {
			map.put("message", "兴趣名称不能为空！");
			map.put("code", 1);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 兴趣爱好删除
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/hobbydelete", produces = "application/json; charset=UTF-8")
	public String hobbydelete(String id, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (!CommonUtils.isEmptyString(id)) {
			userInfoService.deleteHobbyById(CommonUtils.parseLong(id, 0));
			map.put("code", 0);
		} else {
			map.put("code", 1);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 等级信息
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lvinfo", produces = "application/json; charset=UTF-8")
	public ModelAndView lvinfo(String id, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<LvInfo> result = userInfoService.pageLvs(pageIndex);
		if (result != null) {
			mav.addObject("list", result.getList());
			mav.addObject("page", result.getPageModel());
		}
		return mav;
	}
	/**
	 * 
	 * @Description 举报信息
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/accuseinfo", produces = "application/json; charset=UTF-8")
	public ModelAndView accuseinfo(String id, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<ReportInfo> result = userInfoService.pageReport(pageIndex);
		if (result != null) {
			mav.addObject("list", result.getList());
			mav.addObject("page", result.getPageModel());
		}
		return mav;
	}

	/**
	 * 
	 * @Description 等级新增、修改
	 * @param id
	 * @param exp
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lvinfoedit", produces = "application/json; charset=UTF-8")
	public String lvinfoedit(String id, Integer exp) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(id)) { // 修改
			long uid = CommonUtils.parseLong(id, 0);
			LvInfo info = userInfoService.getLvinfoById(uid);
			if (info != null) {

				int lv = info.getLv();

				info.setExp(exp);
				info.setLv(lv);
				// 找到上一个的等级
				LvInfo lastlv = userInfoService.getLvInfoByLv(lv - 1);
				int total_count = 0;
				if (lastlv != null) {
					total_count = lastlv.getTotal() + lastlv.getExp();
				}
				info.setTotal(total_count);
				userInfoService.updateLvinfo(info);

				// 一下的total全变
				List<LvInfo> lvinfo = userInfoService.getFirstLv(lv);
				if (lvinfo != null && lvinfo.size() > 0) {

					for (int i = 1, len = lvinfo.size(); i < len; i++) {
						total_count += lvinfo.get(i - 1).getExp();
						lvinfo.get(i).setTotal(total_count);
						userInfoService.updateLvinfo(lvinfo.get(i));
					}
				}

				map.put("code", 0);
			}

		} else { // 新增
			LvInfo lastlv = userInfoService.getLastLv();
			int lv = 1;
			if (lastlv != null) {
				lv = lastlv.getLv() + 1;
			}

			LvInfo info = new LvInfo();
			info.setLv(lv);
			info.setExp(exp);
			if (lv == 1) {
				info.setTotal(0);
			} else {
				// 找到前一个
				LvInfo golv = userInfoService.getLvInfoByLv(lv - 1);
				if (golv != null) {
					info.setTotal(golv.getTotal() + golv.getExp());
				}
			}
			userInfoService.addlvinfo(info);
			map.put("code", 0);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 等级删除
	 * @param lvs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lvinfodelete", produces = "application/json; charset=UTF-8")
	public String lvinfodelete(String lvs, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (!CommonUtils.isEmptyString(lvs)) {

			// 获取最后一个等级
			LvInfo info = userInfoService.getLastLv();
			if (info != null) {
				int lastlv = info.getLv();
				// 判断lvs的长度，第一个数据以及最后一个数据
				String[] lvstrs = lvs.split(",");
				int lvlen = lvstrs.length;
				if (lvlen == 1) {
					if (lvstrs[0].equals(lastlv + "")) {
						// 执行删除
						userInfoService.deleteLvinfoByLv(lastlv);
						map.put("code", 0);
					} else {
						// 不执行删除
						map.put("code", 1);
						map.put("message", "请从最后一个等级开始删除，不要跨等级删除！");
					}
				} else {
					int last = Integer.parseInt(lvstrs[lvlen - 1]);
					int first = Integer.parseInt(lvstrs[0]);
					int big = last;
					if (last < first) {
						big = first;
					}
					if ((big == lastlv) && (Math.abs(last - first) == (lvlen - 1))) {
						// 执行删除
						userInfoService.deleteLvinfoByLvs(lvs);
						map.put("code", 0);
					} else {
						// 不执行删除
						map.put("code", 1);
						map.put("message", "请从最后一个等级开始删除，不要跨等级删除！");
					}
				}
			}

		} else {
			map.put("code", 1);
			map.put("message", "请选择删除对象");
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 通过user_id 获取匿名用户信息
	 * @param user_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anonymousinfo", produces = "application/json; charset=UTF-8")
	public String anonymousinfo(String user_id) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (!CommonUtils.isEmptyString(user_id)) {
			UserAnonymous info = userInfoService.getAnonymousByUserid(CommonUtils.parseLong(user_id, 0));
			if (info != null) {
				map.put("obj", info);
				map.put("code", 0);
			} else {
				map.put("code", 1);
			}
		} else {
			map.put("code", 1);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 匿名用户登录
	 * @param mobile
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anonymouslogin", produces = "application/json; charset=UTF-8")
	public String anonymouslogin(String mobile, String password) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		// 匿名用户登录，返回匿名信息，使用匿名账号密码登录环信
		UserInfo user = userInfoService.login(mobile);
		if (user != null && user.getPassword().equals(CommonUtils.MD5(password).toLowerCase())) {
			// 返回匿名信息
			map.put("user", user);
			UserAnonymous anonymous = userInfoService.getAnonymousByUserid(user.getId());
			if (anonymous != null) {
				map.put("anonymous", anonymous);
				map.put("code", 0);
			} else {
				// 注册匿名用户
				map.put("code", 1);
			}
		} else {
			map.put("code", 1);
			map.put("message", "用户名密码错误！");
		}
		return JSON.toJSONString(map);
	}

	@ResponseBody
	@RequestMapping(value = "/userseale", produces = "application/json; charset=UTF-8")
	public String userSeale(String userid,String seale) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		UserInfo user = userInfoService.getById(CommonUtils.parseLong(userid, 0));
		if (user != null) {
			int sea = CommonUtils.parseInt(seale, 0);
			if (sea==0) {
				user.setIs_seale(0);
			}
			if (sea==1) {
				user.setIs_seale(1);
			}
			if (sea==2) {
				user.setIs_seale(2);
			}
			userInfoService.updateUserInfo(user);
			map.put("code", 0);
		} else {
			map.put("code", 1);
			map.put("message", "用户不存在！");
		}
		return JSON.toJSONString(map);
	}

	@ResponseBody
	@RequestMapping(value = "/sethome", produces = "application/json; charset=UTF-8")
	public String sethome(String id,String content) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		UserInfo user = userInfoService.getById(CommonUtils.parseLong(id, 0));
		if (user != null) {
			if (user.getIs_home() == 0) {//置顶
				user.setIs_home(1);
				user.setTop_time(new Date());
//				if (!CommonUtils.isEmptyString(content)) {
//					user.setContent(CommonUtils.parseInt(content, 0));//设置顶顺序
//				}
			} else {
				user.setIs_home(0);//取消置顶
				
			}
			userInfoService.updateUserInfo(user);
			map.put("code", 0);
		} else {
			map.put("code", 1);
			map.put("message", "用户不存在！");
		}
		return JSON.toJSONString(map);
	}
	
	@ResponseBody
	@RequestMapping(value = "/homeuserinfo", produces = "application/json; charset=UTF-8")
	public ModelAndView homeuserinfo(String id, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<UserInfo> result = userInfoService.Homeuser(pageIndex);
		if (result != null) {
			mav.addObject("list", result.getList());
			mav.addObject("page", result.getPageModel());
		}
		return mav;
	}
}
